<template>
  <div class="xtx-city">
    <!-- 展示选择结果位置 -->
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder" v-if="!seled.fullSels">请选择配送地址</span>
      <span class="value" v-else>{{ seled.fullSels }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 选择省市区位置 -->
    <div class="option" v-if="active">
      <!-- 只渲染了第一层 -->
      <span
        class="ellipsis"
        @click="selCity(i)"
        v-for="i in cityList"
        :key="i.code"
      >
        {{ i.name }}
      </span>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, reactive } from 'vue'
import axios from 'axios'
// 获取城市列表数据
function useCityList () {
  const cityList = ref([])
  // 备份数据变量
  const backupCityList = []
  const getCityList = async () => {
    const { data: res } = await axios({ url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json', method: 'GET' })
    console.log('城市列表', res)
    backupCityList.push(...res)
    cityList.value = res
  }
  onMounted(() => {
    getCityList()
  })
  return { cityList, backupCityList }
}
export default {
  name: 'XtxCity',
  emits: ['change'],
  setup (props, { emit }) {
    // 1.控制弹层是否显示
    const active = ref(false)
    const { cityList, backupCityList } = useCityList()
    // 打开城市选择
    const open = () => {
      // 还原数据
      cityList.value = backupCityList
      active.value = true
    }
    // 关闭城市选择
    const close = () => {
      active.value = false
    }
    const toggleDialog = () => {
      if (active.value) {
        close()
      } else {
        open()
      }
    }
    /**
   * 点击选择省市区
   * 1.默认展示省
   * 2.打开弹层，点击省展示省下的所有的市 level:1
   * 3.继续点击省展示省下的区 level:2
   */
    const seled = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countryCode: '', // 地区code
      countryName: '', // 地区名
      // 最终选择(省+市+区)
      fullSels: ''
    })
    const selCity = (currCity) => {
      console.log(currCity)
      // 联动的实现
      cityList.value = currCity.areaList
      // 选择的时候存储省市区选中的数据
      if (currCity.level === 0) {
        // 存省
        seled.provinceCode = currCity.code
        seled.provinceName = currCity.name
      } else if (currCity.level === 1) {
        // 存市
        seled.cityCode = currCity.code
        seled.cityName = currCity.name
      } else {
        // 存区
        seled.countryCode = currCity.code
        seled.countryName = currCity.name
        // 拼接完整地址
        seled.fullSels = seled.provinceName + ' ' + seled.cityName + ' ' + seled.countryName
        // 选完关闭弹层
        toggleDialog()
        // 把选择数据传出组件
        emit('change', seled)
      }
    }
    return { active, toggleDialog, cityList, selCity, seled }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
